﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Workflow.Library;
using System.Data.SqlClient;
using Portal.Core;
using Portal.Web.Utility;
using Portal.Workflow.Model;
using System.Data;

namespace Portal.Workflow.Bussiness
{
    public class WorkflowUtilities
    {
        static WorkFlowApp CreateWf(string SiteId, string WorkflowDefId)
        {
            if (string.IsNullOrEmpty(WorkflowDefId)) return null;
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string XmlData = new WorkflowBll().GetXmlWorkflowDataFromDef(OSiteParam.ConnectionString, WorkflowDefId);
            if (String.IsNullOrEmpty(XmlData)) return null;
            WorkFlowApp wf = new WorkFlowApp(XmlData);
            return wf;
        }

        public static WorkflowTaskItemCls[] ReadingMyTask(string SiteId, string UserId, string RoleId)
        {
            SiteParam OSiteParam=WebEnvironments.CreateSiteParam(SiteId);
            Model.WsInParamCls oWsInParamCls=new Model.WsInParamCls();
            oWsInParamCls.ConnectionString = OSiteParam.ConnectionString;
            oWsInParamCls.SiteId=SiteId;
            DataSet ds = WorkflowBll.ReadingMyTask(oWsInParamCls, null, UserId, RoleId);
            WorkflowTaskItemCls[] WorkflowTaskItems = WorkflowTaskItemParser.ParseFromDataTable(ds.Tables[0]);
            return WorkflowTaskItems;
        }

        public static WorkflowTaskItemCls[] ReadingMyTaskForCategory(string SiteId, string UserId, string RoleId, string CatId)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            Model.WsInParamCls oWsInParamCls = new Model.WsInParamCls();
            oWsInParamCls.ConnectionString = OSiteParam.ConnectionString;
            oWsInParamCls.SiteId = SiteId;
            DataSet ds = WorkflowBll.ReadingMyTaskForCat(oWsInParamCls, null, UserId, RoleId, CatId);
            WorkflowTaskItemCls[] WorkflowTaskItems = WorkflowTaskItemParser.ParseFromDataTable(ds.Tables[0]);
            return WorkflowTaskItems;
        }


        //public static WorkflowHasSentCls[] ReadingWorkflowHasSent(string SiteId, string UserId, string RoleId,DateTime dFromDate,DateTime dToDate)
        //{
        //    SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
        //    Model.WsInParamCls oWsInParamCls = new Model.WsInParamCls();
        //    oWsInParamCls.ConnectionString = OSiteParam.ConnectionString;
        //    oWsInParamCls.SiteId = SiteId;
        //    DataTable dtResult = WorkflowBll.ReadingWorkflowTaskHasSent(OSiteParam.ConnectionString, dFromDate, dToDate);
        //    WorkflowHasSentCls[] WorkflowHasSents = WorkflowHasSentParser.ParseFromDataTable(dtResult);
        //    return WorkflowHasSents;
        //}


        //public static WorkflowHasReceivedCls[] ReadingWorkflowHasReceived(string SiteId, string UserId, string RoleId,DateTime dFromDate,DateTime dToDate)
        //{
        //    SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
        //    Model.WsInParamCls oWsInParamCls = new Model.WsInParamCls();
        //    oWsInParamCls.ConnectionString = OSiteParam.ConnectionString;
        //    oWsInParamCls.SiteId = SiteId;
        //    DataTable dtResult = WorkflowBll.ReadingWorkflowTaskHasReceived(OSiteParam.ConnectionString, dFromDate, dToDate);
        //    WorkflowHasReceivedCls[] WorkflowHasReceiveds = WorkflowHasReceivedParser.ParseFromDataTable(dtResult);
        //    return WorkflowHasReceiveds;
        //}



        public static bool CheckHasStartWorkflow(string SiteId,string RecordId)
        {
            return WorkflowBll.CheckHasStartWorkflow(SiteId, RecordId);
        }

        public static Portal.Workflow.Bussiness.WorkflowBll.GetWorkflowCls GetWorkflowActionInfo(string SiteId, string RecordId)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            return WorkflowBll.GetWorkflowActionInfo(OSiteParam.ConnectionString, RecordId); 
        }
       
        public static void StartWorkflow(
            IDbTransaction OSqlTransaction,
            string SiteId,
            string WorkflowDefId, 
            string AssetCode, 
            string RecordId,
            string ComeFromRoleId, 
            string GroupBatchId,
            string BatchId, 
            string ComeFromLoginName,
            string ComeFromFullName,
            string Subject,
            string ObjectType,
            string ExtraData)
        {
            if (string.IsNullOrEmpty(AssetCode)) throw new Exception("Chưa xác định mã AssetCode!");
            if (string.IsNullOrEmpty(ComeFromRoleId)) throw new Exception("Chưa xác định vai trò!");
            if (string.IsNullOrEmpty(Subject)) throw new Exception("Chưa xác định tiêu đề!");
            WorkFlowApp wf = CreateWf(SiteId, WorkflowDefId);
            if (wf != null)
            {
                StartWorkflowParamCls oStartWorkflowParamCls = new StartWorkflowParamCls();
                oStartWorkflowParamCls.WorkflowDefId = wf.WorkFlowDefCls.WorkFlowDefId;
                oStartWorkflowParamCls.GroupBatchId = GroupBatchId;
                oStartWorkflowParamCls.BatchId = BatchId;
                oStartWorkflowParamCls.RecordId = RecordId;
                oStartWorkflowParamCls.ComeFrom = ComeFromLoginName;
                oStartWorkflowParamCls.ComeFromName = ComeFromFullName;
                oStartWorkflowParamCls.ComeFromOfficeCode = AssetCode;
                oStartWorkflowParamCls.ComeFromRoleId = ComeFromRoleId;
                oStartWorkflowParamCls.Subject = Subject;
                oStartWorkflowParamCls.DeleteBeforeNext = wf.WorkFlowDefCls.DeleteBeforeNext;
                oStartWorkflowParamCls.ExpireDate = System.DateTime.Now.AddDays(2);
                oStartWorkflowParamCls.XmlWorkflowData = wf.WorkFlowDefCls.XmlWorkflowData;
                oStartWorkflowParamCls.Owner = ComeFromLoginName;
                oStartWorkflowParamCls.ExtraInfo = ObjectType;
                oStartWorkflowParamCls.ExtraData = ExtraData;
                oStartWorkflowParamCls.PreviewUrl = "#";

                //chay thang khong qua webservice...!
                new WorkflowBll().StartWorkflow(SiteId, (SqlTransaction)OSqlTransaction, oStartWorkflowParamCls);
            }
        }
    }
}
